package com.rede.didiok.admin.service.impl;

import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.admin.dao.RoleDao;
import com.rede.didiok.admin.entity.AdminEntity;
import com.rede.didiok.admin.entity.RoleEntity;
import com.rede.didiok.admin.service.AdminService;
import com.rede.didiok.admin.service.RoleService;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.admin.RoleDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.utils.RedisUtil;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;


@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminService adminService;


    /***************************** admin:start **************************/
    // region  didiok-admin

    @Override
    public IPage<RoleEntity> getPageList(RoleDto roleDto) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(roleDto.getKeyword()) && StringUtils.isNotEmpty(roleDto.getKeyword().trim())) {
            queryWrapper.like(SQLConf.ROLENAEM, roleDto.getKeyword().trim());
        }
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        Page<RoleEntity> page = new Page<>();
        page.setCurrent(roleDto.getCurrentPage());
        page.setSize(roleDto.getPageSize());
        IPage<RoleEntity> pageList = roleService.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public String addRole(RoleDto roleDto) {
        String roleName = roleDto.getRoleName();
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.ROLENAEM, roleName);
        RoleEntity getRole = roleService.getOne(queryWrapper);
        if (getRole == null) {
            RoleEntity role = new RoleEntity();
            role.setRoleName(roleDto.getRoleName());
            role.setCategoryMenuUids(roleDto.getCategoryMenuUids());
            role.setSummary(roleDto.getSummary());
            role.insert();
            return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
        }
        return ResultUtil.errorWithMessage(MessageConf.ENTITY_EXIST);
    }

    @Override
    public String editRole(RoleDto roleDto) {
        String uid = roleDto.getUid();
        RoleEntity getRole = roleService.getById(uid);
        if (getRole == null) {
            return ResultUtil.errorWithMessage(MessageConf.PARAM_INCORRECT);
        }
        getRole.setRoleName(roleDto.getRoleName());
        getRole.setCategoryMenuUids(roleDto.getCategoryMenuUids());
        getRole.setSummary(roleDto.getSummary());
        getRole.setUpdateTime(new Date());
        getRole.updateById();
        // 修改成功后，需要删除redis中所有的admin访问路径
        deleteAdminVisitUrl();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String deleteRole(RoleDto roleDto) {
        // 判断该角色下是否绑定了管理员
        QueryWrapper<AdminEntity> blogQueryWrapper = new QueryWrapper<>();
        blogQueryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        blogQueryWrapper.in(SQLConf.ROLEUID, roleDto.getUid());
        Integer adminCount = adminService.count(blogQueryWrapper);
        if (adminCount > 0) {
            return ResultUtil.errorWithMessage(MessageConf.ADMIN_UNDER_THIS_ROLE);
        }
        RoleEntity role = roleService.getById(roleDto.getUid());
        role.setStatus(StatusEnum.DISABLED);
        role.setUpdateTime(new Date());
        role.updateById();
        deleteAdminVisitUrl();
        return ResultUtil.successWithMessage(MessageConf.DELETE_SUCCESS);
    }

    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    // endregion
    /***************************** web:end **************************/


    /**
     * 删除Redis中管理员的访问路径
     */
    private void deleteAdminVisitUrl() {
        Set<String> keys = redisUtil.keys(RedisConf.ADMIN_VISIT_MENU + "*");
        redisUtil.delete(keys);
    }
}